package com.yanqu.road.server.logic.activity.stronghold.condition.union;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdActivityModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.stronghold.StrongholdActivityData;
import com.yanqu.road.server.logic.activity.stronghold.condition.BaseStrongholdActivityUnionRankCondition;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityRankMgr;
import com.yanqu.road.utils.string.StringUtils;

public class StrongholdActivityDamageUnionRankCondition extends BaseStrongholdActivityUnionRankCondition {
    public StrongholdActivityDamageUnionRankCondition(GamePlayer player, StrongholdActivityData activityData, ActivityConditionInfo conditionInfo) {
        super(player, activityData, conditionInfo);
    }

    @Override
    public void addTrigger(GamePlayer player) {
           player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.StrongholdEliminateRank.getValue(), handle);

    }

    @Override
    public void removeTrigger(GamePlayer player) {
            player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.StrongholdEliminateRank.getValue(), handle);

    }

    @Override
    public void initDefaultValue() {
        String unionUid = player.getUserInfo().getUnionUid();
        if (!StringUtils.isNullOrEmpty(unionUid)) {
            UserActivityConditionData userActivityConditionData = activityData.getUserActivityConditionData(getConditionId());
            userActivityConditionData.setUnionUid(unionUid);
            StrongholdActivityRankMgr.changeUnionActivityValueNotUpload(activityData.getActivityId(), conditionInfo.getType(), unionUid, player.getUserId(), userActivityConditionData,UnionRiseRankArgs.NOTIFY_TYPE_NORMAL);
            player.getModule(StrongholdActivityModule.class).onActivityDataChange(activityData);
        }
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        return false;
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        UnionRiseRankArgs args = (UnionRiseRankArgs) object;
        if (null != args) {
            ActivityInfo activityInfo = StrongholdActivityMgr.getInTimeActivityInfo(activityData.getActivityId());
            if (null != activityInfo && !StrongholdActivityMgr.isGameFinish()) {
                int notifyType = args.getType();
                String unionUid = args.getUnionUid();
                UserActivityConditionData userActivityConditionData = activityData.getUserActivityConditionData(getConditionId());
                if (UnionRiseRankArgs.NOTIFY_TYPE_NORMAL == notifyType || UnionRiseRankArgs.NOTIFY_TYPE_JOIN == notifyType) {
                    userActivityConditionData.setUnionUid(player.getUserInfo().getUnionUid());
                } else if (UnionRiseRankArgs.NOTIFY_TYPE_REMOVE == notifyType || UnionRiseRankArgs.NOTIFY_TYPE_DELETE == notifyType) {
                    userActivityConditionData.setUnionUid("");
                }
                StrongholdActivityRankMgr.changeUnionActivityValueNotUpload(activityData.getActivityId(), conditionInfo.getType(), unionUid, player.getUserId(), userActivityConditionData, notifyType);
                player.getModule(StrongholdActivityModule.class).onActivityDataChange(activityData);
            }
        }
    }
}
